package com.epro.dx.util;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;

import com.epro.dx.activity.creditcard.AddCreditCardActivity;
import com.epro.dx.activity.creditcard.CreditCardListActivity;
import com.epro.dx.listenner.GetCreditCardInfoListenner;
import com.weipu.common.constants.Constant;
import com.weipu.common.constants.ServerConstant;
import com.weipu.common.facade.exception.DxException;
import com.weipu.common.facade.factory.ProviderFactory;
import com.weipu.common.facade.model.CreditCardInfo;
import com.weipu.common.facade.model.SimpleCreditCard;
import com.weipu.common.util.AlertToast;

public class GetCreditCardInfoAsyncTask extends AsyncTask<Void, Void, String> {

	private CreditCardInfo creditCardInfo;

	private GetCreditCardInfoListenner infoListenner;
	private Activity context;
	private int pageIndex;
	
	public GetCreditCardInfoAsyncTask(GetCreditCardInfoListenner infoListenner,Activity context,int pageIndex) {
	       this.infoListenner=infoListenner;
	       this.context=context;
	       this.pageIndex = pageIndex;
	}

	@Override
	protected void onPreExecute() {
		// showLoadingLayout(null);
		if(null!=infoListenner){
			infoListenner.onPreGetInfo();
		}
	}

	@Override
	protected String doInBackground(Void... params) {
		try {
			creditCardInfo = ProviderFactory.createCreditCardServiceProvider()
					.getCreditCardInfos(
					ActivityUtil.getCustomerID(),
					ActivityUtil.getCustomerToken(), false, pageIndex,
							Constant.LIST_PAGE_COUNT);
		} catch (DxException e) {
			return e.getErrCode();
		}
		return "0";
	}

	@Override
	protected void onPostExecute(String result) {
		if(!context.isFinishing()){
			if(null!=infoListenner){
				infoListenner.onGetInfoFinish();
			}
			if ("0".equals(result) && null != creditCardInfo) {
				infoListenner.onGetInfoComplect(creditCardInfo);
//				ArrayList<SimpleCreditCard> simpleCreditCards = (ArrayList<SimpleCreditCard>) creditCardInfo
//						.getsCreditCards();
//				Intent intent = new Intent();
//				String type = creditCardInfo.getGatewayType();
//				if (null == simpleCreditCards || simpleCreditCards.size() == 0) {
//					intent.setClass(context, AddCreditCardActivity.class);
//				} else {
//					intent.setClass(context, CreditCardListActivity.class);
//					intent.putExtra("CardFlow", type);
//					intent.putParcelableArrayListExtra("CreditCards",
//							simpleCreditCards);
//				}
//				intent.putExtra("CardFlow", type);
//				intent.putExtra(ServerConstant.Parameter.PARAMETER_NAME_ORDER_NUMBER, orderNumber);
//				context.startActivity(intent);
			} else {
				AlertToast.alert(context,
						ServerConstant.ReturnCode.MESSAGEMAP.get(result));
			}
		}
	}

}
